package com.demoo.zjsu.controller.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebSelectDao;
import com.demoo.zjsu.model.smartwebsite.DetailInfo;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.Summarize;
import com.demoo.zjsu.util.HttpClientUtil.PostSender;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/27.
 * 还是主页，科学研究之类的代码
 */
@Controller
public class ZJGSUController {


    @Autowired
    SmartWebSelectDao dao;

    @RequestMapping("/smartweb/info/service")
    public ModelAndView toInformationService() {
        List<PageTwo> pageTwos = dao.selectPageTwoByCategory("信息服务");
        return new ModelAndView("smartweb/tel-number").addObject("page", pageTwos);
    }


    @RequestMapping("/smartweb/info/score")
    public ModelAndView toScore() {
        return new ModelAndView("smartweb/score");
    }


    @RequestMapping("/smartweb/info/client")
    @ResponseBody
    public ModelAndView client(@RequestParam("ssSelect") String ssSelect, @RequestParam("klSelect") String klSelect) {
        String[] key = {"ssSelect", "klSelect"};
        String[] val = {ssSelect, klSelect};
        String result = PostSender.send("http://zhaoban.zjsu.edu.cn/abjcx.asp", key, val);
        Elements elements = Jsoup.parse(result).getAllElements();
        String desc = "省市为“" + ssSelect + "”科类为“" + klSelect + "”";
        Elements table1 = elements.select("[width=92%]").select("tr");
        List<Summarize> summarizes = new ArrayList<Summarize>();
        int i = 0;
        for (Element element : table1) {

            Summarize summarize = new Summarize();
            summarize.setId(i);
            i++;
            Elements td = element.select("td");
            summarize.setYear(td.eq(0).text());
            summarize.setSubject(td.eq(1).text());
            summarize.setRenshu(td.eq(2).text());
            summarize.setZhongdian(td.eq(3).text());
            summarize.setErben(td.eq(4).text());
            summarize.setToudang(td.eq(5).text());
            summarize.setMin(td.eq(6).text());
            summarize.setMax(td.eq(7).text());
            summarize.setAver(td.eq(8).text());
            summarize.setRate(td.eq(8).text());
            summarize.setPici(td.eq(9).text());
            summarizes.add(summarize);
        }

        List<DetailInfo> detailInfos = new ArrayList<DetailInfo>();
        Elements table2 = elements.select("[width=94%]").select("tr");
        for (Element element : table2) {
            DetailInfo detailInfo = new DetailInfo();
            Elements td = element.select("td");
            detailInfo.setId(i);
            i++;
            detailInfo.setYear(td.eq(0).text());
            detailInfo.setName(td.eq(1).text());
            detailInfo.setCategory(td.eq(2).text());
            detailInfo.setMin(td.eq(4).text());
            detailInfo.setMax(td.eq(5).text());
            detailInfo.setAver(td.eq(6).text());
            detailInfo.setPici(td.eq(7).text());
            detailInfo.setRenshu(td.eq(8).text());
            detailInfos.add(detailInfo);
        }
        return new ModelAndView("smartweb/score").addObject("desc", desc)
                .addObject("summarizes", summarizes).addObject("detailinfo", detailInfos);
    }

}
